<template>
	<view :style="{paddingBottom:'20rpx' }">
		<view class="fixed">
			<!-- 选择日期 -->
			<uni-datetime-picker :show="showDate" type="date" v-model="selectDate" :end="endData" ref="dateTimePicker" @change="dateChange">
				<u-cell-group :border="false">
					<u-cell 
						rightIconStyle="width:24rpx;height:24rpx"
						titleStyle='font-size:28rpx;color:#3B3E45' :isLink="true" @click="selectDateClick">
						<template #title>
							<view class="u-slot-title">
								<text class="u-cell-text">选择日期：</text>
								<text class="u-cell-desc">
									{{selectDate ? selectDate :'请选择日期'}}
								</text>
							</view>
						</template>
					</u-cell>
				</u-cell-group>
			</uni-datetime-picker>

			<!-- tab -->
			<view class="pub-tab d-f">
				<view v-for="(item,index) in tabList" :key="index" class="tab-item"
					:class="tabIndex ===index ? 'active' :''" @click="tabClick(index)">
					{{item.name}}
				</view>
			</view>
		</view>
		<view class="list">
			<view class="list-item" v-for="(newsitem,index) in formatlist" :key='index'>
				<view class="history-date">
					{{newsitem.sctime}}
				</view>
				<view v-for="(item, index) in newsitem.list" :key="index" class="news-item" @click="navToDetails(item)">
					<view class="title"><text class="list-badge"></text> <text
							:style="isInArray(clickedid,item.id)?'color:gray':''">{{item.title}}</text></view>
					<view class="infor">{{item.contentText}}</view>
					<view class="bot">
						<view class="bot-text author">
							<image class="type-icon" src="/static/img/home/zb-icon.png"></image>{{item.typeStr}}
						</view>
						<view class="bot-text area">
							<image class="type-icon" src="/static/img/home/diqu-icon.png"></image>{{item.diqu}}
						</view>
						<view class="bot-text time red">
							<image class="type-icon" src="/static/img/home/time-icon.png"></image>{{item.sctime}}
						</view>
						<!-- 附件 -->
						<view class="bot-text file" v-if="item.fujian==2">
							<image class="type-icon" src="/static/img/home/fujian.png"></image>附件
						</view>
						<!-- 标书 -->
						<view class="bot-text biaoshu" v-if="item.fujian==1">
							<image class="type-icon" src="/static/img/biaoshu.png"></image> 标书
						</view>
					</view>
				</view>
			</view>
		</view>
		<list-loading :status.sync="status" :listdata="historyList" :isBottom.sync="isScrollBottm"
			:defindNull="!historyList.length" v-if="tabIndex==0">
			<view class="nullWrap">
				<image src="https://img.bidcenter.com.cn/uni/zanwushuju.png" mode="aspectFit">
				</image>
			</view>
		</list-loading>
		<list-loading :status.sync="status" :listdata="historyList" :isBottom.sync="isScrollBottm" v-if="tabIndex==1"
			:defindNull="!historyList.length">
			<view class="nullWrap">
				<image src="https://img.bidcenter.com.cn/uni/zanwushuju.png" mode="aspectFit">
				</image>
			</view>
		</list-loading>
	</view>
</template>

<script>
	const d = new Date()
	const year = d.getFullYear()
	let month = d.getMonth() + 1
	month = month < 10 ? `0${month}` : month
	const date = d.getDate()
	export default {
		data() {
			return {
				location: 8058,
				showDate: false,
				selectDate: '',
				endData: `${year}-${month}-${date}`,
				tabIndex: 0,
				tabList: [{
						name: "招标信息",
					},
					{
						name: "项目信息",
					},
				],
				historyList: [],
				formatlist: [],
				title: '浙江卓宏建设项目管理有限公司关于绍兴市柯桥区实景三维数据建设',
				zbpageindex: 1,
				gcpageindex: 1,
				page: 1,
				isScrollBottm: false,
				status: 'loading',
				loadingText: '努力加载中...',
				// 自动加载，目前此状态用不到
				loadmoreText: '加载更多',
				nomoreText: '已全部加载完毕',
				isShowLoading: true,
				pageSize: 40,
				clickedid: []
			}
		},
		async mounted() {
			await this.getList()
		},


		methods: {
			// 页面滚动到底部的事件（不是scroll-view滚到底），常用于下拉下一页数据
			scrollBottom() {
				this.isScrollBottm = true;
				if (this.historyList.length < 10) {
					return
				}
				if (this.status !== 'nomore') {
					this.getList({
							page: ++this.page
						},
						true
					);
				}
			},
			//打开日期组件
			selectDateClick() {
				this.$refs.dateTimePicker.show()
				this.showDate = true
			},
			// 点击日期组件确定按钮
			dateChange(e) {
				this.selectDate = e
				this.zbpageindex = 1
				this.gcpageindex = 1
				this.page = 1;
				this.showDate = false;
				this.historyList = [];
				this.formatlist = [];
				this.getList();
			},
			// 点击日期组件关闭按钮
			dateClose() {
				this.showDate = false
			},
			// 招标、项目切换
			tabClick(index) {
				this.tabIndex = index
				this.page = 1
				if (this.tabIndex === 0) {
					this.zbpageindex = 1
				} else {
					this.gcpageindex = 1
				}
				this.historyList = [];
				this.formatlist = [];
				this.isScrollBottm = false
				uni.pageScrollTo({
					scrollTop: 0, //滚动到页面的目标位置
					duration: 0
				});
				this.getList();
			},
			isInArray(arr, value) {
				for (var i = 0; i < arr.length; i++) {
					if (value === arr[i]) {
						return true;
					}
				}
				return false;
			},

			//跳转到详情页
			navToDetails(item) {
				this.clickedid.push(item.id);
				let url = "";
				let uri = encodeURIComponent(JSON.stringify(item));
				switch (item.type) {
					case 3:
					case 10:
					case 11:
					case 12:
					case 13:
					case 14:
					case 98:
					case 99:
						uni.navigateTo({
							url: '/pages/xiangmu/detail?ly=shoucang&data=' + encodeURIComponent(uri)
						})
						break;
					default:
						uni.navigateTo({
							url: '/pages/zhaobiao/detail?ly=shoucang&data=' + encodeURIComponent(uri)
						})
						break;
				}
			},

			// 是否显示 loading，初始化请求接口显示，滚动到底部加载下一页时显示A
			getList(params = {}, loadmore = false) {
				// 非下拉加载时
				if (!loadmore) {
					this.status = 'loading';
					this.nomoreText = "努力加载中..."
					this.historyList = [];
					this.formatlist = [];
				}
				const paramsNew = Object.assign({}, {
						type: this.tabIndex,
						page: 1,
						zbpage: this.zbpageindex,
						gcpage: this.gcpageindex,
						pagesize: this.pageSize,
						location: this.location,
						startdate: this.selectDate,
						enddate: this.selectDate
					},
					params
				);
				uni.$u.http
					.post('User/liulan/ListHandler.ashx', paramsNew)
					.then(rs => {
						const dt = rs.list || [];
						// console.log(55,dt)
						if (!dt.length) {
							this.status = 'nomore';
							this.nomoreText = loadmore ? '已全部加载完毕' : '暂无数据'
							this.isScrollBottm = false
							return;
						} else {
							if (dt.length < this.pageSize) {
								this.status = 'nomore';
								this.nomoreText = '已全部加载完成'
							} else {
								this.status = 'loading';
							}
						}
						dt.map(item => {
							this.historyList.push(item);
						})



						this.formatlist = this.formattingData(this.historyList, 'sctime')
						if (this.tabIndex === 0) {
							this.zbpageindex++
						} else {
							this.gcpageindex++
						}

					})
					.catch(err => {
						this.status = 'nomore';
						// err.errMsg: 响应错误，如：404  err.msg：状态码retbs 非0
						uni.$u.toast(err.msg || '列表接口异常!');
					});
			},
			formattingData(arr, group_key) {
				// 先定义一个空对象和空数组；
				let map = {};
				let res = [];
				// 循环需要筛选的数组
				for (let i = 0; i < arr.length; i++) {
					let ai = arr[i];
					// 将需要筛选的属性的值作为新对象的键，并且判断是否已经存在
					if (!map[ai[group_key]]) {
						// 不存在的话就在map对象中创建一个属性的值作为键名，键值为空数组的新对象，并且把arr[i]放入
						map[ai[group_key]] = [ai];
					} else {
						// 如果已经存在就直接把arr[i]放入
						map[ai[group_key]].push(ai);
					}
				}
				// 循环后对map进行处理生成分组的数组
				Object.keys(map).forEach((key) => {
					res.push({
						[group_key]: key,
						list: map[key],
					});
				});

				return res;
			},
		},
		computed: {
			newsTypeMap: function() {
				const ininData = JSON.parse(JSON.stringify(this.$store.state.init.newsTypeData));
				const obj = {};
				ininData.map(item => {
					obj[item.key] = item.value;
				});
				return obj;
			},
		}
	}
</script>

<style lang="scss" scoped>
	@import '@/static/css/liulanjilu.scss';
	@import "@/static/css/commonList.scss";

	.productTitle {
		.title {
			font-weight: bold;
		}

		.tag {
			display: inline-block;
		}

	}

	.nullWrap {
		display: flex;
		justify-content: center;
		padding-top: 50rpx;
	}

	.pub-tab {
		width: 100%;
		background: #fff;
		margin-top: -2rpx;

		.tab-item {
			width: 50%;
			height: 96rpx;
			line-height: 96rpx;
			font-size: 32rpx;
			color: #3B3E45;
			text-align: center;
			position: relative;

			&::after {
				content: '';
				width: 0rpx;
				height: 6rpx;
				background: #2F6DFE;
				border-radius: 4rpx;
				position: absolute;
				left: 50%;
				bottom: 0rpx;
				margin-left: -29rpx;
				-webkit-transition: width 0.13s ease-in-out;
				transition: width 0.13s ease-in-out;
			}

			&.active {
				color: #2F6DFE;

				&::after {
					width: 58rpx;
				}
			}
		}
	}

	.fixed {
		width: 100%;
		position: sticky;
		top: 97rpx;
		left: 0;
		z-index: 99;
		background-color: #fff;
	}

	.list {
		// margin-top: 198rpx;
		padding-bottom: 20rpx;

		.list-item {
			margin: 0 28rpx 0;

			.news-item {
				margin: 20rpx 0 0;
				padding: 24rpx 20rpx;
				width: 100%;

				.bot {
					justify-content: initial;

					.bot-text {
						margin-right: 10rpx;
						align-items: center;
						display: flex;
						font-size: 22rpx;
					}
				}
			}
		}

		.list-data {
			padding: 28rpx 24rpx 24rpx;
			background: #fff;
			border-radius: 10rpx;
			margin-top: 20rpx;
		}

		.title-box {
			.title {
				text {
					text-indent: 120rpx;
					line-height: 40rpx;
					font-size: 32rpx;
					color: #3B3E45;
					margin-left: 4rpx;
					font-weight: bold;
				}
			}
		}

		.infor-box {
			justify-content: space-between;
			margin-top: 2rpx;
			align-items: flex-end;
		}

		.time {
			//margin-top: 10rpx;
			line-height: 40rpx;
			color: #6C717E;
		}

		.tag {
			margin-top: 10rpx;
		}
	}

	.history-date {
		height: 90rpx;
		line-height: 90rpx;
		font-size: 28rpx;
		color: #ADB4C2;
		margin-bottom: -20rpx;
	}

	.fujian {
		width: 24rpx;
		height: 24rpx;
	}

	.u-line {
		border-color: #eee !important;
	}
</style>
<style lang="scss">
	page {
		background: #FAFAFA;
	}

	.u-cell__body {
		padding: 30rpx !important;
		background: #fff;
	}

	.u-cell-text {
		font-size: 28rpx;
		color: #3B3E45;
	}

	.u-cell-desc {
		// margin-left: 8rpx;
		font-size: 28rpx;
		color: #ADB4C2;
	}
</style>